﻿<template>
  <el-container>
    <el-header>
      <home-header ref="homeHeader" :current-user="admin" @enter="onHeaderEnter" />
    </el-header>
    <el-container>
      <home-nav-menu ref="navMenu" :height="height - 60" />
      <el-main class="home-main">
        <home-tabs ref="tabs" :pane-height="paneHeight" />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import Vue from 'vue';
import '@/components/index'; // 公共vue组件，应全部打包到本入口文件只有一份
import HomeHeader from './HomeHeader';
import HomeNavMenu from './HomeNavMenu';
import HomeTabs from './HomeTabs';
import PageManager from './PageManager';
import { formatPermissions } from '@/auth/permission';
import { start } from './auto-start';
import config from '@/config/app.config';
import session from '@/auth/session';
import { isFullScreen } from '@/utils/fullscreen'
export default {
  components: {
    HomeHeader,
    HomeNavMenu,
    HomeTabs
  },
  mixins: [ PageManager ],
  data() {
    return {
      width: document.body.offsetWidth,
      height: document.body.offsetHeight,
      admin: null
    };
  },
  computed: {
    paneHeight() {
      return this.height - 103;
    }
  },
  async mounted() {
    // 对于非页面组件，仍要使用pushPage
    Vue.prototype.pushPage = this.pushPage;

    // 暴漏给以前的（未使用webpack前）代码
    window.app = this;
    
    // 在从登陆进来，admin已获取，或者可能刷新浏览器
    if (!session.admin()) {
      const ret = await this.$http.get('/api/currentUser', {timeout: 5000}).catch(() => {
        return Promise.resolve(null);
      });
      if (!(ret && ret.admin)) {
        this.$router.push('/login');
        return;
      }
      session.login(ret.admin);
    }
    this.admin = session.admin();

    this.$refs.navMenu.loadMenus(this.admin.permissions);

    this.admin.permissions = formatPermissions(this.admin.permissions);

    // 提供给老代码用
    window.admin = this.admin;

    if (!config.get('navMenuExpanded')) {
      start.bind(this)(this.admin);
    } else {
      app.$refs.navMenu.$once('expanded', () => {
        start.bind(this)(this.admin);
      });
    }

    // 获取字典数据
    DictMan.fetch();

    window.dispatchEvent(new CustomEvent('EnterHome'));

    document.addEventListener('keydown', this.onKeyDown);
    window.addEventListener('resize', this.onResize);
    this.$once('hook:beforeDestroy', () => {
      document.removeEventListener('keydown', this.onKeyDown);
      window.removeEventListener('resize', this.onResize);
    });
  },
  methods: {
    onHeaderEnter() {
      this.$refs.navMenu.perform();
    },
    onKeyDown(event) {
      const srcElType = event.srcElement.constructor;
      if (!(srcElType == HTMLBodyElement || srcElType == HTMLDocument)) {
        return;
      }
      if (27 == event.keyCode) {
        const { tabs } = this.$refs;
        if (tabs.tabs.length > 0) {
          tabs.removeTab(tabs.tabs[tabs.tabs.length - 1].key);
        } else if (location.hash != '#/login') {
          this.logout();
        } else {
          location.reload();
        }
      }
    },
    onResize() {
      this.width = document.body.offsetWidth;
      this.height = document.body.offsetHeight;
    }
  }
}
</script>

<style scoped>
.el-container {
  width: 100%;
  background-image: none;
}
</style>
<style>
.home-main.el-main {
  padding: 0px 0px 0px 0px;
  background: #f0f2f5;
}
</style>